package com.ruyuan.eshop.common.service.trade.cart.impl;

import com.ruyuan.eshop.common.model.goods.vo.SpecValueVO;
import com.ruyuan.eshop.common.model.trade.cart.enums.CartType;
import com.ruyuan.eshop.common.model.trade.cart.enums.CheckedWay;
import com.ruyuan.eshop.common.model.trade.cart.vo.CartSkuVO;
import com.ruyuan.eshop.common.model.trade.cart.vo.CartVO;
import com.ruyuan.eshop.common.model.trade.cart.vo.CartView;
import com.ruyuan.eshop.common.model.trade.cart.vo.PriceDetailVO;
import com.ruyuan.eshop.common.service.trade.cart.CartReadService;
import com.ruyuan.eshop.common.service.trade.cart.cartbuilder.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车只读操作业务类
 */
@Service
public class CartReadServiceImpl implements CartReadService {


    private final Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 购物车促销渲染器
     */
    @Autowired
    private CartPromotionRenderer cartPromotionRenderer;

    /**
     * 购物车价格计算器
     */
    @Autowired
    private CartPriceCalculator cartPriceCalculator;

    /**
     * 购物车sku数据渲染器
     */
    @Autowired
    @Qualifier(value = "cartSkuRendererImpl")
    private CartSkuRenderer cartSkuRenderer;

    /**
     * 数据校验
     */
    @Autowired
    private CheckDataRebderer checkDataRebderer;

    /**
     * 购物车优惠券渲染器
     */
    @Autowired
    private CartCouponRenderer cartCouponRenderer;

    /**
     * 购物车运费价格计算器
     */
    @Autowired
    private CartShipPriceCalculator cartShipPriceCalculator;


    /**
     * 读取购物车数据，并计算优惠和价格
     * @param way 获取方式
     * @return 购物车构建器最终要构建的数据
     */
    @Override
    public CartView getCartListAndCountPrice(CheckedWay way) {

        // 正常是要从购物车存储数据中读取数据 TODO

        // 模拟一份购物车数据返回 FIXME
        CartView cartView = new CartView();
        List<CartVO> cartList = new ArrayList<>();
        CartVO cartVO = new CartVO();
        cartVO.setSellerId(1L);
        cartVO.setShippingTypeId(null);
        cartVO.setShippingTypeName(null);
        cartVO.setSellerName("儒猿技术窝");
        cartVO.setWeight(1.0);
        PriceDetailVO priceDetailVO = new PriceDetailVO();
        priceDetailVO.setTotalPrice(6550.0);
        priceDetailVO.setOriginalPrice(6550.0);
        priceDetailVO.setGoodsPrice(6550.0);
        priceDetailVO.setFreightPrice(0.0);
        priceDetailVO.setDiscountPrice(0.0);
        priceDetailVO.setCashBack(0.0);
        priceDetailVO.setFullMinus(0.0);
        priceDetailVO.setIsFreeFreight(0);
        priceDetailVO.setExchangePoint(0L);
        cartVO.setPrice(priceDetailVO);
        List<CartSkuVO> skuList = getSkuList();
        cartVO.setSkuList(skuList);
        cartVO.setChecked(1);
        cartVO.setCartType(CartType.CART);
        cartList.add(cartVO);
        cartView.setCartList(cartList);
        cartView.setCouponList(null);
        cartView.setTotalPrice(getTotalPrice());
        return cartView;
    }

    private PriceDetailVO getTotalPrice() {
        PriceDetailVO priceDetailVO = new PriceDetailVO();
        priceDetailVO.setTotalPrice(6550.0);
        priceDetailVO.setOriginalPrice(6550.0);
        priceDetailVO.setGoodsPrice(6550.0);
        priceDetailVO.setFreightPrice(0.0);
        priceDetailVO.setDiscountPrice(0.0);
        priceDetailVO.setCashBack(0.0);
        priceDetailVO.setFullMinus(0.0);
        priceDetailVO.setIsFreeFreight(0);
        priceDetailVO.setExchangePoint(0L);
        return priceDetailVO;
    }

    private List<CartSkuVO> getSkuList() {

        List<CartSkuVO> list = new ArrayList<>();
        CartSkuVO cartSkuVO = new CartSkuVO();
        cartSkuVO.setSellerId(1L);
        cartSkuVO.setSellerName("儒猿技术窝");
        cartSkuVO.setGoodsId(1417677439226580994L);
        cartSkuVO.setSkuId(1417746396428079105L);
        cartSkuVO.setSkuSn("hm-01-000001");
        cartSkuVO.setCatId(497L);
        cartSkuVO.setNum(1);
        cartSkuVO.setPurchaseNum(0);
        cartSkuVO.setGoodsWeight(1.0);
        cartSkuVO.setOriginalPrice(6550.0);
        cartSkuVO.setPurchasePrice(6550.0);
        cartSkuVO.setSubtotal(6550.0);
        cartSkuVO.setName("鸿蒙手机");
        cartSkuVO.setGoodsImage("http://javashop-01.oss-cn-shanghai.aliyuncs.com/javashop/goods/BEF25E136AE049D481770A703340D286.jpeg_300x300");
        cartSkuVO.setChecked(1);
        cartSkuVO.setIsFreeFreight(1);
        cartSkuVO.setNotJoinPromotion(0);
        cartSkuVO.setTemplateId(0L);
        cartSkuVO.setSpecList(getSpecList());
        cartSkuVO.setPoint(0);
        cartSkuVO.setServiceStatus("NOT_APPLY");
        cartSkuVO.setLastModify(1627312179L);
        cartSkuVO.setEnableQuantity(180);
        cartSkuVO.setInvalid(0);
        cartSkuVO.setIsShip(1);
        cartSkuVO.setGoodsType("NORMAL");
        list.add(cartSkuVO);
        return list;
    }


    private List<SpecValueVO> getSpecList() {
        List<SpecValueVO> specList = new ArrayList<>();
        SpecValueVO specValueVO1 = new SpecValueVO();
        specValueVO1.setSellerId(1L);
        specValueVO1.setSpecValue("黄色");
        specValueVO1.setSellerId(null);
        specValueVO1.setSpecName("颜色");
        specValueVO1.setSkuId(null);
        specValueVO1.setSpecValueId(188L);
        specList.add(specValueVO1);


        SpecValueVO specValueVO2 = new SpecValueVO();
        specValueVO2.setSpecId(1417744751111671809L);
        specValueVO2.setSellerId(null);
        specValueVO2.setSpecValue("8+128");
        specValueVO2.setSellerId(null);
        specValueVO2.setSpecName("存储容量");
        specValueVO2.setSkuId(null);
        specValueVO2.setSpecValueId(1417744797169324033L);
        specList.add(specValueVO2);

        return specList;
    }

    /**
     * 由缓存中取出已勾选的购物列表<br>
     * @param way 获取方式
     * @return 购物车构建器最终要构建的数据
     */
    @Override
    public CartView getCheckedItems(CheckedWay way) {

        return getCartListAndCountPrice(way);

    }


}
